package cn.btks.resultintegration.rule.controller;

import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.resultintegration.rule.dto.export.ProjectExport;
import cn.btks.resultintegration.rule.dto.rule.ExternalSystemParam;
import cn.btks.resultintegration.util.CryptoTestUtils;
import cn.btks.resultintegration.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author lijun
 * @Date 2021/6/28 15:15
 */
@RestController
@RequestMapping("api/v1.0/resultIntegration/rule")
@Api(tags = "规则模块")
@Validated
public class ExternalSystemController {

    @Value("${external.system}")
    private String url;

    // 接口地址
//    static final String url = "http://10.100.248.171:8040/share/signApi/";//环保专网
//    static final String url = "http://59.255.14.40:8040/share/signApi/";//电子政务外网
    static final String EKey = "8HkocpYLeG1LNi5m";
    /**
     * 正式环境接口授权key(见文档)
     */
    static final String appId = "a722dc60a2894f960566a939301a4a19";
    /*公钥*/
    static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCFQx9fjXsPHBMJV/qNDDyhWhBcZFUcBW+vXGMzADqAj8W3WFKjnyaF6LMou5QKmm6q5z1XK6u7+EZCPzIARJYFDcJDRA2tqUq1UOjF2n8/Vmi+go+sFzy/tJyQv/OUQQOSl05cbk+Iydb8Eapm/hHi6XxX3m/YV4hVSRsfKlxavwIDAQAB";


    @PostMapping("/externalSystem")
    @ApiOperation("对接环评审批系统")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "sign", value = "签名", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "data", value = "各接口请求参数json对象", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "time", value = "时间戳", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "service", value = "业务接口名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "version", value = "接口版本号", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "appId", value = "应用唯一标识", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAllByCounty(@RequestBody ExternalSystemParam externalSystemParam, HttpServletRequest request) throws IOException {

        String version = "1.0";
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String data = externalSystemParam.getData();
        System.out.println("data----------------->"+data);
        String sign = CryptoTestUtils.encryptByPublicKey(org.apache.commons.codec.digest.DigestUtils.md5Hex(externalSystemParam.getService() + time + data + version + EKey),publicKey);
        Map param = new HashMap() {{
            put("version", version);
            put("time", time);
            put("sign", sign);
            put("data", CryptoTestUtils.encryptByPublicKey(data, publicKey));
            put("appId", appId);
        }};

        System.out.println("data------------>" + CryptoTestUtils.encryptByPublicKey(data, publicKey));
        System.out.println("sign------------>" + sign);
        System.out.println(HttpUtil.getUrlBuilder(url + externalSystemParam.getService(), param));
        String result1 = HttpUtil.httpPost(url + externalSystemParam.getService(), param);
        System.out.println(result1);
        JSONObject json = JSON.parseObject(result1);
        String res = CryptoTestUtils.decryptByPublicKey(json.getString("body"), publicKey);
        System.out.println("解密后的结果：" + res);
        return !StringUtils.isEmpty(res) ? new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.SAVE_OK.getMsg(), res):
                new ResultState(StateInfo.FIND_ERROR.getCode(), StateInfo.FIND_ERROR.getMsg());
    }
}
